﻿$("div.login-btn-tabs > a").click(function () {
    if ($(this).text().indexOf('企业') > -1) {
        $('#isEnterpriceRegister').val('true');
        $('#enterpriceCodeContainer').show();        
    }
    else {
        $('#isEnterpriceRegister').val('false');
        $('#enterpriceCodeContainer').hide();
    }
    $(this).addClass("active").siblings().removeClass("active");
});

//$("#intlCode").selectinput({
//    Offset: [5, 5],
//    autowidth: true,
//    css: {
//        rootclass: "ui-select",
//        headclass: "selected-value",
//        mouseon: "mouseon",
//        current: "current"
//    }
//});

$('#verifyImage').attr("src", verifyImageSrc + '?r=' + Math.random()).click(function () {
    $(this).attr("src", verifyImageSrc + '?r=' + Math.random());
});

// bug56463
if ($('#smsCodeContainer .verify-info').text().length > 0) {
    $('#verifycode').val('');
}

$('input[type="password"]').togglePassword();

$('#btnSendSMSCode').click(function () {
    if (!$('#mobile').valid()) {
        $('#mobile').focus();
        return;
    }

    if ($("#verifyCodePic:visible").length > 0 && !$('#verifyCodePic').valid()) {
        $('#verifyCodePic').focus();
        return;
    }

    $(this).attr("disabled", "disabled").addClass('btn-verifyCode-dis');

    $.ajax({
        url: "/PortalSite/SendSMSVerificationCode",
        type: "POST",
        data: {
            "mobile": $('#mobile').val(),
            "verifyCode": $('#verifyCodePic').val()
        },
        success: function (data) {
            if (data.IsSuccess) {
                $('#smsCodeInfo').text(data.Message).show();
                $('#smsCodeContainer').find('.controls-hint').show();
                $('#verifyImageContainer, #smsCodeContainer').find('.verify-info').html('').hide();

                resetSendWaiting($('#btnSendSMSCode'), $('#smsCodeInfo'), $('#verifyImage'));
            }
            else {
                $('#verifyImage').click();
                $('#verifyCodePic').val('');
                $('#btnSendSMSCode').removeAttr("disabled").removeClass('btn-verifyCode-dis');;

                if (data.ErrorCode == -99) { // 图片验证码错误
                    $('#verifyImageContainer').find('.verify-info').html(data.Message).show();
                }                
                else {
                    if (data.ErrorCode == 30062) { // 获取手机验证码次数太多
                        if ($("#smsVerifyImageContainer").css('display') == 'none') {
                            $("#smsVerifyImageContainer").show();
                            //addSMSVerifyCodeValidateRule();
                        }
                    }

                    // 主动开启倒计时60s
                    if (data.ErrorCode == -1 && data.Message.indexOf('60秒') > -1) {
                        resetSendWaiting($('#btnSendSMSCode'), $('#smsCodeInfo'), $('#verifyImage'));
                    }

                    $('#smsCodeContainer').find('.controls-hint').hide();
                    $('#smsCodeContainer').find('.verify-info').html(data.Message).show();
                }
            }
        }
    });
});

$("#registerSumbit").click(function () {
    if ($("#chkFW")[0].checked) {
        if ($('#registerForm').valid()) {
            $(this).attr("disabled", "disabled");
            $(this).val('注册中');
            $('#registerForm').submit();
        }        
    }
    else {
        alert("请先勾选《途家网服务协议》");
        return false;
    }
})

$("#exampleInputAccount4").validate({
    rules: {
        mobile: {
            required: true,
            mobileRegex: true
        },
        verifyCodePic: {
            required: true,
            minlength: 4,
            maxlength: 4
        },
        verifycode: {
            required: true,
            minlength: 4,
            maxlength: 4,
            number: true
        },
        email: {
            maxlength: 50,
            email: true
        },        
        password: {
            required: true,
            rangelength: [6, 16]
        },
        fromId: {
            number: true
        },
        enterpriceCode: {
            required: true,
            enterpriseRegex: true

        }
    },
    messages: {
        mobile: {
            required: "请输入手机号",
            mobileRegex: "手机号格式不正确"
        },
        verifyCodePic: {
            required: "请输入验证码",
            minlength: "验证码不正确",
            maxlength: "验证码不正确"
        },
        verifycode: {
            required: "请输入手机验证码",
            minlength: "手机验证码不正确",
            maxlength: "手机验证码不正确",
            number: "手机验证码不正确"
        },
        email: {
            maxlength: "您的邮箱名太长了",
            email: "邮箱格式不正确"
        },        
        password: {
            required: "请输入密码",
            rangelength: jQuery.validator.format("请输入长度为 {0} 到 {1} 位的密码"),
        },
        fromId: {
            number: "邀请码不正确"
        },
        enterpriceCode: {
            required: "请输入企业ID",
            enterpriseRegex: "企业ID不正确"
        }
    },
    onfocusout: function(e) {
        this.element(e);
    },
    onkeyup: false,
    errorClass: "error",
    errorPlacement: function (error, element) {
        var fieldContainer = element.parent().parent().parent();
        fieldContainer.find('.controls-hint').hide();
        fieldContainer.find('.verify-info').html(error.text()).show();        
    },
    success: function (label) {        
        var fieldContainer = $('#' + label.attr('for')).parent().parent().parent()
        fieldContainer.find('.controls-hint').show();
        fieldContainer.find('.verify-info').html('').hide();
    }
});